﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Windows.Data;

namespace Urb.Weather.Ticker.UI.Converters
{
    public abstract class RoundNullableNumberConverter<T> : IValueConverter where T : struct
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
                return "-";
            
            if (!(value is Nullable<T>))
                return "Invalid";

            var number = (Nullable<T>)value;

            if (!number.HasValue)
                return "-";

            return Round(number.Value);
        }

        protected abstract int Round(T value);

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }
    
    [ValueConversion(typeof(decimal?), typeof(string))]
    public class RoundNullableDecimalConverter : RoundNullableNumberConverter<decimal>
    {
        protected override int Round(decimal value)
        {
            return (int) Math.Round(value);
        }
    }
}
